// pages/account/all_coupons/index.js
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgurl: app.urlimg,
        select: 0,
        header: [],
        add: [],
        last_box: [],
        scrollHeight: 0,
        likegoods: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.requestHeader();
    },

    /**
     * 头部点击事件
     */
    head_click: function (e) {
        var that = this;
        var title = e.currentTarget.dataset.title;
        var id = e.currentTarget.dataset.index;
        this.invoking(title);
        that.setData({
            select: id,
            scrollHeight: 0,
        })
        this.requestCommdities(id);
    },

    /**
     * 根据选中的标题，发送相应数据请求
     */
    invoking: function (title) {
        this.setData({
            add: []
        });
        switch (title) {
            case '优惠券': this.getCouponList(); break;
            case '叠加金': this.getRepeatList(); break;
            case '立减金': this.getCouponList2(); break;
            default: ;
        }
    },

    // 领券
    btns: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var add = that.data.add;

        var selectTitle = "";
        for (var i in that.data.header) {
            if (that.data.header[i].id == that.data.select) {
                selectTitle = that.data.header[i].title;
                break;
            }
        }
        var requestUrl = "";
        var data = {};
        if (selectTitle != "") {
            switch (selectTitle) {
                case '优惠券': {
                    requestUrl = "api_coupon/receive";
                    data = { 'type': 0, 'couponId': id }
                }; break;
                case '叠加金': {
                    requestUrl = "api_repeatreduction/receive";
                    data = { 'repeatId': id };
                }; break;
                case '立减金': {
                    requestUrl = "api_coupon/receive";
                    data = { 'type': 1, 'repeatId': id };
                }; break;
                default: ; break
            }
        }
        if (requestUrl) {
            wx.request({
                url: app.url + requestUrl,
                data: data,
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                }, success: function (data) {
                    console.log(data);
                }
            })
        }

        var text = '', status = '';
        for (var i in add) {
            if (add[i].id == id) {
                if (add[i].restricts > 0) {
                    --add[i].restricts;
                    text = '领取成功', status = 'success';
                } else {
                    text = '领取失败', status = 'loading';
                }
            }
        }

        wx.showToast({
            title: text,
            icon: status,
            duration: 1200,
        })
        that.setData({
            add: add
        })
    },
    /**
     * 获取头部导航
     */
    requestHeader: function () {
        var that = this;
        wx.request({
            url: app.url + "api_reductioncenter/getCenterList",
            data: {
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            }, success: function (json) {
                var select = json.data.result[0] ? json.data.result[0].id : 0;
                var title = json.data.result[0] ? json.data.result[0].title : "";
                that.setData({
                    header: json.data.result,
                    select: select
                });
                that.invoking(title);
                that.requestCommdities(select);
            }
        })
    },

    /**
     * 获取推荐商品 
     */
    requestCommdities: function (id) {
        var that = this;
        wx.request({
            url: app.url + 'api_reductioncenter/getAdAndCommodities',
            data: {
                type: id
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            success: function (json) {
                that.setData({
                    last_box: json.data.result.addList,
                    likegoods: json.data.result.commodities
                });
            }
        })
    },
    goods_click: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url,
        })
    },
    //
    click_goods: function (e) {
        var url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url,
        });
    },

    /**
     * 请求叠加金
     */
    getRepeatList: function () {
        var that = this;
        wx.request({
            url: app.url + 'api_repeatreduction/repeatList',
            data: {
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            success: function (json) {
                that.setData({
                    add: json.data.result,
                })
            }
        })
    },

    /**
     * 请求优惠券
     */
    getCouponList: function () {
        var that = this;
        wx.request({
            url: app.url + 'api_coupon/couponTopic',
            data: {
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            success: function (json) {
                that.setData({
                    add: json.data.result,
                })
            }
        })
    },

    /**
     * 请求立减金
     */
    getCouponList2: function () {
        var that = this;
        wx.request({
            url: app.url + 'api_coupon/reducefundlist',
            data: {
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            },
            success: function (json) {
                that.setData({
                    add: json.data.result.couponList,
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

})